<?php

class Users_AdminController extends Zend_Controller_Action
{
	public function deleteAclCache(){
		$cache = Zend_Registry::get('Zend_Cache_Core');
		$cache->remove('ACL_rights');
	}

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $modelGrid = new Webergate_Grid();
		$grid = $modelGrid->getGrid();

		$modelUsers = new Application_Model_Users();
        $grid->query( $modelUsers->getAllUsers( 'query' ) );

		$form = new Bvb_Grid_Form('Users_Form_Users');
		$form->setDelete(true);
			 //->setAdd(true)->setEdit(true)->setAddButton(true);
		$grid->setForm($form);

		$grid->updateColumn('id',array('remove'=>true));
		$grid->setDeleteConfirmationPage(true);

		$grid->setExport(array());
		$this->view->pages = $grid->deploy();
    }

	public function rolesAction()
	{
		$modelGrid = new Webergate_Grid();
		$grid = $modelGrid->getGrid();

		$modelRoles = new Application_Model_AclRole();
        $grid->query( $modelRoles->getAllRoles( 'query' ) );

		$form = new Bvb_Grid_Form('Users_Form_Roles');
		$form->setIsPerformCrudAllowed(true);
		$form->setAdd(true)->setEdit(true)->setDelete(true)->setAddButton(true);
		$form->setCallbackAfterInsert( array($this, 'deleteAclCache') );
		$form->setCallbackAfterDelete( array($this, 'deleteAclCache') );
		$form->setCallbackAfterUpdate( array($this, 'deleteAclCache') );
		$grid->setForm($form);

		if ( $this->getRequest()->getParam('edit') || $this->getRequest()->getParam('add') ){
			$gridForm = $grid->getForm(1);
			$modelRoles = new Application_Model_AclRole();
			$elementParentId = $gridForm->getElement('parent_id');
			$currentParentValue = 0;
			if ( $this->getRequest()->getParam('edit') ){
				$currentParentValue = $gridForm->getElement('ZFPK')->getValue();
			}
			$elementParentId->addMultiOptions( $modelRoles->getRecurringRoles( 0, $currentParentValue ) );
		}

		$grid->setDeleteConfirmationPage(true);
        $grid->setUseKeyEventsOnFilters(true);
		$grid->setRecordsPerPage(10);

        $grid->setExport(array());

        $this->view->pages = $grid->deploy();
	}

	public function rightsAction(){
		$modelRights = new Application_Model_AclRights();
		// rights data grid
		$modelRoles = new Application_Model_AclRole();
		$roleId = $this->getRequest()->getParam('roleId');
		$rootRoleId = $modelRoles->getRootId();
		if ( !$roleId ){
			$roleId = $rootRoleId;
		}

		// regenerate resources
		$modelAclResources = new Application_Model_AclResources();
		$modelAclResources->generateResourceTable();
		// save changes
		if ( $this->getRequest()->getParam('actionP') == 'save' ){
			$newRights = $this->getRequest()->getParam('rights');
			$modelRights->delete( 'acl_role_id = ' . $roleId );
			//Zend_Debug::dump($newRights);die();
			foreach ( $newRights as $id => $val ){
				if ( $val != -1 ){
					$modelRights->insert( array('acl_role_id'=>$roleId, 'acl_resource_id'=>$id, 'permission'=>$val) );
				}
			}
			$this->deleteAclCache();
			$this->_redirect('/users/admin/rights/roleId/' . $roleId);
		}

		$rowRole = $modelRoles->getRoleById( $roleId );
		//Zend_Debug::dump($rowRole);exit;

		$formSelectRole = new Zend_Form();
		$element = new Zend_Form_Element_Select('roleId');
		$element->setValue( $roleId )
				->addMultiOptions( $modelRoles->getRecurringRoles(0) )
				->setAttrib('onChange', "submitFrm('select')");
		$formSelectRole->addElement( $element );
		$this->view->formSelectRole = $formSelectRole;

		$modelGrid = new Webergate_Grid();
		$grid = $modelGrid->getGrid();

		$arrayGridRights = $modelRights->getRightsForRole( $rowRole['name'], $rowRole['parent'] );
		$source = new Bvb_Grid_Source_Array($arrayGridRights);
		$grid->setSource($source);

		$extra_right = new Bvb_Grid_Extra_Column();
        $extra_right->position('right')
				->name('Drepturi')
				->decorator("<input type='radio' value=\"1\" name='rights[{{id}}]'{{checked_right}}>Allow&nbsp;&nbsp;&nbsp;
					<input type='radio' value=\"0\" name='rights[{{id}}]'{{checked_deny}}>Deny&nbsp;&nbsp;
					" . ( $rootRoleId != $roleId ? "<input type='radio' value=\"-1\" name='rights[{{id}}]'{{checked_inherit}}>Inherit":"")) ;
		$grid->addExtraColumns($extra_right);

		if ( $rootRoleId != $roleId ){
			$extra_parent_right = new Bvb_Grid_Extra_Column();
			$extra_parent_right->position('right')
					->name('Drepturi mostenite')
					->decorator("<input type='checkbox' value=\"1\" name='{{name}}' value='1'{{checked_parent_right}} disabled='disabled' />");
			$grid->addExtraColumns($extra_parent_right);
		}
		$grid->updateColumn('id',array('remove'=>true));
		$grid->updateColumn('checked_right',array('remove'=>true));
		$grid->updateColumn('checked_deny',array('remove'=>true));
		$grid->updateColumn('checked_inherit',array('remove'=>true));
		$grid->updateColumn('checked_parent_right',array('remove'=>true));


//		$form = new Bvb_Grid_Form('Users_Form_Roles');
//		$form->setEdit(true)->setDelete(true)->setAdd(true);
//		$form->setCallbackAfterUpdate( array($this, 'deleteAclCache') );
//		$grid->setForm($form);
//
//		if ( $this->getRequest()->getParam('edit') ){
//			$gridForm = $grid->getForm(1);
//			$modelRoles = new Application_Model_AclRole();
//			$elementParentId = $gridForm->getElement('parent_id');
//			$elementParentId->addMultiOptions( $modelRoles->getRecurringRoles( 0, $gridForm->getElement('ZFPK')->getValue() ) );
//		}
//
//		$grid->setDeleteConfirmationPage(true);
        $grid->setUseKeyEventsOnFilters(true);
		$grid->setRecordsPerPage(50);

        $grid->setExport(array());

        $this->view->dataGrid = $grid->deploy();
	}

}